package com.jonsnowsword.wifiassistant;

import java.util.List;

import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;

public class WifiConnect {
	public static WifiConfiguration CreateWifiInfo(String SSID, String Password, String Type,WifiManager wifiManager){  
        WifiConfiguration config = new WifiConfiguration();    
         config.allowedAuthAlgorithms.clear();  
         config.allowedGroupCiphers.clear();  
         config.allowedKeyManagement.clear();  
         config.allowedPairwiseCiphers.clear();  
         config.allowedProtocols.clear();  
        config.SSID = "\"" + SSID + "\"";    
         
        WifiConfiguration tempConfig = IsExsits(SSID,wifiManager);            
        if(tempConfig != null) {   
      	  wifiManager.removeNetwork(tempConfig.networkId);   
        } 
         
        if(Type.equals("1")) //WIFICIPHER_NOPASS 
        {  
             config.wepKeys[0] = "";  
             config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);  
             config.wepTxKeyIndex = 0;  
        }  
        if(Type.equals("2")) //WIFICIPHER_WEP 
        {  
            config.hiddenSSID = true; 
            config.wepKeys[0]= "\""+Password+"\"";  
            config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);  
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);  
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);  
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);  
            config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);  
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);  
            config.wepTxKeyIndex = 0;  
        }  
        if(Type.equals("3")) //WIFICIPHER_WPA 
        {  
        config.preSharedKey = "\""+Password+"\"";  
        config.hiddenSSID = true;    
        config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);    
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);                          
        config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);                          
        config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);                     
        //config.allowedProtocols.set(WifiConfiguration.Protocol.WPA);   
        config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 
        config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 
        config.status = WifiConfiguration.Status.ENABLED;    
        } 
         return config;  
  }  

		public static WifiConfiguration IsExsits(String SSID,WifiManager wifiManager) { // �鿴��ǰ�Ƿ��Ѿ����ù��SSID  
	        List<WifiConfiguration> existingConfigs = wifiManager.getConfiguredNetworks();
	        if(existingConfigs==null)
	        	return null;
	        for (WifiConfiguration existingConfig : existingConfigs) {
	        	if(existingConfig==null)
	        		continue;
	        	else if(existingConfig.SSID==null) {
	        		continue;
	        	}
	        	else if(existingConfig.SSID.equals("\"" + SSID + "\""))
				{
	                return existingConfig;
	            }
	        }
	        return null;
	    }

		public static String pdjm(String Capabilities){ 
       	 	if(Capabilities.equals("[ESS]"))       	 	{
       	 		return "1";
            }
       	 	else if(Capabilities.equals("[WEP][ESS]")){
            	return "2";
            }
       	 	else{
            	return "3";
            }
	   }
}
